<?php

namespace addons\TfFutures\backend\controllers;

use yii\data\Pagination;
use Yii;
use common\models\common\SearchModel;

use common\helpers\ExcelHelper;
use common\models\member\Member;
use addons\TfFutures\common\models\OrderReason;

class OrderReasonController extends BaseController
{
    public $modelClass = OrderReason::class;

    /**
     * 首页
     * @return string
     */
    /*public function actionList()
    {
        //time search
        $start_time ='';
        $end_time = '';
        $where = [];
        if(Yii::$app->request->get()){
            $start_time  = Yii::$app->request->get('start_time',
                date('Y-m-d H:i:s', strtotime("-60 day")));
            $end_time    = Yii::$app->request->get('end_time',
                date('Y-m-d H:i:s', strtotime("+1 day")));
            $username    = Yii::$app->request->get('username');
            $state    = Yii::$app->request->get('state');
            if($username){
                $member = Member::find()->where(['username'=>$username])->one();
                if(empty($member)){
                    return $this->message("没有此用户", $this->redirect(['index']), 'error');
                }else{
                    $where['member_id'] = $member->id;
                }
            }elseif($state){
                $where['state'] = $state;
            }elseif ($start_time && $end_time){
                $start_time = strtotime($start_time);
                $end_time = strtotime($end_time);
            }
        }
        $searchModel = new SearchModel([
            'model'                  => $this->modelClass,
            'scenario'               => 'default',
            'relations'              => ['member_info' => ['username']],
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder'           => [
                'open_time' => SORT_DESC,
            ],
            'pageSize'               => $this->pageSize,
        ]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        if(!empty($start_time && $end_time)){
            if($where == null){
                $where = ['>', 'rf_addon_tf_futures_order_reason.id', 0,];
            }
            $dataProvider->query->andFilterWhere($where)
                ->andFilterWhere(['between', 'rf_addon_tf_futures_order_reason.created_at',$start_time ,$end_time ]);
        }else{
            if($where == null){
                $where = ['>', 'rf_addon_tf_futures_order_reason.id', 0,];
            }
            $dataProvider->query->andFilterWhere($where);
        }
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
        ]);
    }*/
    /**
     * 挂单管理
     * @return string
     */
    public function actionIndex()
    {
        //time search
        /*$start_time  = Yii::$app->request->get('start_time',
            date('Y-m-d 00:00:00'));
        $end_time    = Yii::$app->request->get('end_time',
            date('Y-m-d 23:59:59'));*/
        $start_time  = Yii::$app->request->get('start_time');
        $end_time    = Yii::$app->request->get('end_time');
        //$type        = Yii::$app->request->get('type');
        $username    = Yii::$app->request->get('username');
        //$close_type  = Yii::$app->request->get('close_type');

        $searchModel = new SearchModel([
            'model'                  => $this->modelClass,
            'scenario'               => 'default',
            'relations'              => ['member_info' => ['username']],
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder'           => [
                'created_at' => SORT_DESC,
            ],
            'pageSize'               => $this->pageSize,
        ]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        if (!empty($username)) {
            $dataProvider->query->andFilterWhere(['like','username',$username]);
        }

        if($start_time && $end_time){
            $start = strtotime($start_time);
            $end = strtotime($end_time);
            $dataProvider->query->andFilterWhere(['between','rf_addon_tf_futures_order_reason.created_at',$start,$end]);
        }

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
            'start_time'   => $start_time,
            'end_time'     => $end_time,
            //'type'         => $type,
            // 'close_type'   => $close_type,
        ]);
    }

    public function actionExcel()
    {
        $model = $this->modelClass::find()
            ->where(['>', 'id', 0])
            ->all();
        if (empty($model)) {
            return $this->message('没有数据可以被导出', $this->redirect('index'), 'error');
        }
        $header = [
            ['订单ID', 'id', 'text'],
            ['用户名', 'member_info', 'text'],
            ['产品名称', 'symbol_cn', 'text'],
            ['合约量', 'contract_size', 'text'],
            ['设定价格', 'price', 'text'],
            ['止盈金额', 'stop_profit_price', 'text'],
            ['止损金额', 'stop_loss_price', 'text'],
            ['手续费', 'service_fee', 'text'],
            ['保证金', 'deposit', 'text'],
        ];
        foreach ($model as $item) {

            /*switch ($item->rise_fall) {
                case  'RISE':
                    $rise_fall = '涨';
                    break;
                case  'FALL':
                    $rise_fall = '跌';
                    break;
                default:
                    //123
                    break;
            };*/
            $list [] = [
                'id'            => $item->id,
                'member_info'   => $item->member_info->username,
                'symbol_cn'     => $item->symbol_cn,
                'contract_size' => $item->contract_size,
                'price'     => $item->price,
                'stop_profit_price'    => $item->stop_profit_price,
                'stop_loss_price'    => $item->stop_loss_price,
                'service_fee'   => $item->service_fee,
                'deposit'    => $item->deposit,
            ];
        }

        return ExcelHelper::exportData($list, $header);
    }
}